package com.nuc.iblog.controler;

import com.nuc.iblog.MyWebAppConfigurer;
import com.nuc.iblog.entity.Music;
import com.nuc.iblog.entity.Picture;
import com.nuc.iblog.entity.User;
import java.io.File;

import com.nuc.iblog.entity.Video;
import com.nuc.iblog.jpa.MusicJpa;
import com.nuc.iblog.jpa.PictureJpa;
import com.nuc.iblog.jpa.VideoJpa;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;


@Controller
@RequestMapping("cl")
public class UploadController {
    Logger log = LoggerFactory.getLogger(UploadController.class);
    @Autowired
    private PictureJpa pictureJpa;
    @Autowired
    private MusicJpa musicJpa;
    @Autowired
    private VideoJpa videoJpa;
    @ResponseBody
    @RequestMapping("/uploadpic")
    public String uploadpic(Picture picture, @RequestParam("pic")MultipartFile multipartFile, HttpSession httpSession){
        User user=(User)httpSession.getAttribute("User");
        picture.setUser(user);
        String picname=createfilename(multipartFile);
        String picurl=MyWebAppConfigurer.picsPath()+picname;
        picture.setPurl("blogFile/picture/"+picname);
        File file=new File(picurl);
        savepic(multipartFile,file);
        pictureJpa.save(picture);
        return "1";
    }

    @ResponseBody
    @RequestMapping("/uploadheadpic")
    public String uploadheadpic(@RequestParam("pic")MultipartFile multipartFile,HttpSession httpSession){
        log.info("上传用户头像");
        String picname=createfilename(multipartFile);
        String picurl=MyWebAppConfigurer.Pathpic()+picname;
        String picurll="blogFile/"+picname;
        File file=new File(picurl);
        savepic(multipartFile,file);
        log.info("上传用户头像");
        log.info(picurll);
        return picurll;
    }
    public void savepic(MultipartFile multipartFile,File file){
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @RequestMapping("/uploadmusic")
    public String uploadmusic(Music music,@RequestParam("musicfile")MultipartFile multipartFile,@RequestParam("mpic")MultipartFile multipartFile1 ,HttpSession httpSession){
        log.info("用户上传音乐");
        User user=(User)httpSession.getAttribute("User");
        music.setUser(user);
        String musicname=createfilename(multipartFile);
        String musicurl=MyWebAppConfigurer.musicPath()+musicname;
        String musicurll="blogFile/music/"+musicname;
        music.setMurl(musicurll);
        String musicpicname=createfilename(multipartFile1);
        String musicpicurll="blogFile/music/"+musicpicname;
        String musicpicurl=MyWebAppConfigurer.musicPath()+musicpicname;
        music.setMpicurl(musicpicurll);
        File musicfile=new File(musicurl);
        File musicpic=new File(musicpicurl);
        try {
            multipartFile.transferTo(musicfile);
            multipartFile1.transferTo(musicpic);
            musicJpa.save(music);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "1";
    }

    @RequestMapping("uploadvideo")
    public void uploadvideo(Video video, @RequestParam("video")MultipartFile multipartFile, @RequestParam("headpic")MultipartFile multipartFile1 , HttpSession httpSession){
        User user=(User)httpSession.getAttribute("User");
        video.setUser(user);
        String videourl=MyWebAppConfigurer.videoPath()+createfilename(multipartFile);
        video.setVurl(videourl);
        String vpicurl=MyWebAppConfigurer.videoPath()+createfilename(multipartFile1);
        video.setVpicurl(vpicurl);
        File videofile=new File(videourl);
        File vpicfile=new File(vpicurl);
        try {
            multipartFile.transferTo(videofile);
            multipartFile1.transferTo(vpicfile);
            videoJpa.save(video);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String createfilename(MultipartFile file){
        String filename = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String newfilename = UUID.randomUUID() + filename;
        String replace = newfilename.replace("-", "");
        return replace;
    }

}
